﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KMFrame.Permission.NBL.DBEntities;
using KMFrame.Permission.Common;
using KMFrame.Permission.NBL;
using NBearLite;

namespace KMFrame.Permission.BLL
{
    public class Groups : PermissionLogic
    {
        public class GroupInfo
        {
            public int GroupID { get; set; }
            public string GroupName { get; set; }
            public string GroupDescription { get; set; }
        }

        public IEnumerable<KM_UserGroup> GetGroupsList()
        {
            IEnumerable<KM_UserGroup> list = NBLData.Select(DBColumns.KM_UserGroup).ToList<KM_UserGroup>();
            return list;
        }

        public GroupInfo GetGroupInfo(int GroupID)
        {
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                DBColumns.KM_Groups.GroupID, 
                DBColumns.KM_Groups.GroupName,
                DBColumns.KM_Groups.GroupDescription
            };
            return NBLData
                .Select(DBColumns.KM_Groups, selectColumns)
                .Where(DBColumns.KM_Groups.GroupID == GroupID)
                .ToSingleObject<GroupInfo>();
        }

        public IEnumerable<GroupInfo> GetGroupInfoList()
        {
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                DBColumns.KM_Groups.GroupID,
                DBColumns.KM_Groups.GroupName,
                DBColumns.KM_Groups.GroupDescription
            };
            IEnumerable<GroupInfo> list = NBLData
                .Select(DBColumns.KM_Groups, selectColumns)
                .ToList<GroupInfo>();
            return list;
        }

        public bool UpdateGroupInfo(GroupInfo Info)
        {
            bool result = false;
            UpdateSqlSection update = NBLData.Update(DBColumns.KM_Groups)
                .AddColumn(DBColumns.KM_Groups.GroupName, Info.GroupName)
                .AddColumn(DBColumns.KM_Groups.GroupDescription, Info.GroupDescription);
            int ri = update
                .Where(DBColumns.KM_Groups.GroupID == Info.GroupID)
                .Execute();
            if (ri == 1)
                result = true;
            return result;
        }
    }
}
